//package com.car.gatway.filter;
//
//import com.car.gatway.config.LoginProperties;
//import com.car.gatway.exception.AuthenticationException;
//import com.car.commons.core.util.JwtUtil;
//import io.jsonwebtoken.Claims;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.annotation.Order;
//import org.springframework.stereotype.Component;
//import org.springframework.util.AntPathMatcher;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import javax.annotation.Resource;
//import java.util.Date;
//import java.util.List;
//
//@Component
////指定顺序
//@Order(1)
//public class AuthenticationFilter implements GlobalFilter {
//
//    @Resource
//    private LoginProperties loginProperties;
//
//    //路径通配配置类
//    private AntPathMatcher antPathMatcher = new AntPathMatcher();
//
//    /**
//     * 检查路径是否在相应的集合里面
//     * @param path
//     * @param urls
//     * @return
//     */
//    private boolean isCheck(String path, List<String> urls) {
//        for (String url : urls) {
//            if (antPathMatcher.match(url,path)) return true;
//        }
//        return false;
//    }
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        System.out.println("执行登录认证");
//        System.out.println(loginProperties.getBaseUrls());
//        //判定是否需要认证
//        String path = exchange.getRequest().getPath().toString();
//        System.out.println(path);
//        //判定是否在公共放行里面
//        if (this.isCheck(path, loginProperties.getBaseUrls()))return chain.filter(exchange);
//        //判定当前资源是什么类型
//        List<String> values = exchange.getRequest().getHeaders().get("car-token");
//        if (path.endsWith(".pt")) {
//            if (this.isCheck(path, loginProperties.getAdminUrls()))return chain.filter(exchange);
//            if (values == null || values.size() <= 0) throw new AuthenticationException(401,"未登录");
//            String token = values.get(0);
//            try {
//                Claims claims = JwtUtil.parseAdminToken(token);
//                //获取过期时间
//                long expiredTime = claims.getExpiration().getTime();
//                long currentTime = new Date().getTime();
//                if ((expiredTime-currentTime)/1000<60){
//                    throw new AuthenticationException(401,"登录过期");
//                }
//                //通过
//                return chain.filter(exchange);
//            } catch (Exception e) {
//                throw new AuthenticationException(401,"未登录");
//            }
//        } else if (path.endsWith(".md")) {
//            if (this.isCheck(path, loginProperties.getClerkUrls()))return chain.filter(exchange);
//            if (values == null || values.size() <= 0) throw new AuthenticationException(402,"未登录");
//            String token = values.get(0);
//            try {
//                Claims claims = JwtUtil.parseClerkToken(token);
//                //获取过期时间
//                long expiredTime = claims.getExpiration().getTime();
//                long currentTime = new Date().getTime();
//                if ((expiredTime-currentTime)/1000<60){
//                    throw new AuthenticationException(402,"登录过期");
//                }
//                //通过
//                return chain.filter(exchange);
//            } catch (Exception e) {
//                throw new AuthenticationException(402,"未登录");
//            }
//        } else {
//            if (this.isCheck(path, loginProperties.getUserUrls()))return chain.filter(exchange);
//            if (values == null || values.size() <= 0) throw new AuthenticationException(403,"未登录");
//            String token = values.get(0);
//            try {
//                Claims claims = JwtUtil.parseUserToken(token);
//                //获取过期时间
//                long expiredTime = claims.getExpiration().getTime();
//                long currentTime = new Date().getTime();
//                if ((expiredTime-currentTime)/1000<60){
//                    throw new AuthenticationException(403,"登录过期");
//                }
//                //通过
//                return chain.filter(exchange);
//            } catch (Exception e) {
//                throw new AuthenticationException(403,"未登录");
//            }
//        }
//    }
//
//}
